<?php

namespace App\Http\Services\Api\V1\App\Product;

use App\Models\Customers\Customer;
use App\Models\Order\Order;
use App\Models\Order\OrderOption;
use App\Models\Order\OrderProduct;
use App\Models\Order\OrderStatus;
use App\Models\Products\Product;
use App\Models\Products\Review;
use App\Services\Utils\Guard;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

class ReviewService
{
    /**
     * @param $review_type
     * @param $per_page
     * @return Collection|LengthAwarePaginator
     * @throws \Exception
     */
    public function getReview($review_type, $per_page)
    {
        $customer_id = Guard::customer()->customer_id;
        $order_status_ids = OrderStatus::getOrderStatusIdsToBeEvaluate();

        /** @var LengthAwarePaginator|Collection $order_products */
        $order_products = (new OrderProduct)->filterCanBeEvaluate($order_status_ids, $customer_id)
            ->with('review.reviewImages', 'orderOptions', 'product')
            ->when((int)$review_type === Review::ORDER_PRODUCT_REVIEW_HAS, function(Builder $query){
                return $query->has('review');
            })
            ->when((int)$review_type === Review::ORDER_PRODUCT_REVIEW_NO, function(Builder $query){
                return $query->doesntHave('review');
//                    ->doesntHave('returnProduct');
            })
            ->orderBy('order_product_id', 'DESC')
            ->paginate($per_page);

        $this->transformReview($order_products, $review_type);

        return $order_products;
    }

    /**
     * @param LengthAwarePaginator $order_products
     * @param $review_type
     */
    private function transformReview(LengthAwarePaginator $order_products, $review_type)
    {
        $order_products->transform(function(OrderProduct $order_product) use($review_type){

            return [
                'order_id'         => $order_product->order_id,
                'order_product_id' => $order_product->order_product_id,
                'product_id'       => $order_product->product_id,
                'name'             => $order_product->name,
                'product'          => $this->getProduct($order_product),
                'options'          => $this->getOrderOptions($order_product->orderOptions),
                'review'           => $order_product->review,
            ];
        });
    }

    /**
     * @param OrderProduct $order_product
     * @return array
     */
    private function getProduct(OrderProduct $order_product)
    {
        return [
            'product_id'              => $order_product->product_id,
            'image'                   => $order_product->product->image ?? null,
            'origin_image'            => $order_product->product->origin_image ?? null,
        ];
    }

    /**
     * @param Collection $order_options
     * @return Collection
     */
    private function getOrderOptions(Collection $order_options)
    {
        $order_options = $order_options->groupBy('name')->values();

        $order_options->transform(function(\Illuminate\Support\Collection $group_order_options){
            return [
                'name' => $group_order_options->first()->name,
                'value' => $group_order_options->transform(function(OrderOption $order_option){
                    return $order_option->value;
                }),
            ];
        });

        return $order_options;
    }
}